'''
Functions implementing views used to register and login
'''

from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.models import User
from django.core.context_processors import csrf
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from forms import RegisterForm
from mapaimprez.menu import MainLoggedIn, MainLoggedOut
#from django.template import Context, loader
#from auth.models import UserProfile
#from django.contrib.auth.forms import UserCreationForm


def index(request):
    if request.user.is_authenticated():
        return HttpResponse("authenticated")
    else:
        return HttpResponse("not authenticated")
    
    return HttpResponse("Hello, world. You're at the poll index.")


def register_view(request):
    'Generating view that is used for registering users'
    
    if request.user.is_authenticated():
        return HttpResponse("Youre authenticated")
    
    institution = ""
    
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        
        if request.POST.get('institution') != None:
            institution = 'checked="checked"'
        
        if form.is_valid():
            #user = form.save()
            
            user = User.objects.create_user(username=request.POST.get('username'), \
                                 email=request.POST.get('email'), \
                                 password=request.POST.get('password1'))
            
            #user.get_profile().institution = False
            user.get_profile().fullname = ''
            if request.POST.get('institution') == None:
                user.get_profile().role = 'O'
            else:
                user.get_profile().role = 'O'
            
            user.save()
            
            print "A: ", user.username, " B: ", user.password
            
            if user.is_active:
                auth_user = authenticate(username=request.POST['username'], password=request.POST['password1'])
                
                if auth_user is not None:
                    print "auth not none"
                    login(request, auth_user)
                    
                    if auth_user.is_authenticated():
                        print "auth ok"
                    else:
                        print "auth failed"
                else:
                    print "auth is none"
            
            return HttpResponseRedirect("/")
    else:
        form = RegisterForm()
        
    if request.user.is_authenticated():
        menu = MainLoggedIn
    else:
        menu = MainLoggedOut
    
    c = {'form': form ,
         'institution_checked' : institution,
         'menu' : menu,
         'template_menu' : 'menu.html'}
    
    c.update(csrf(request))
    
    return render_to_response("auth/register.html",  c)


def login_view(request):
    'Generating view for logging in'
    
    if request.method == 'POST':
        
        print "after POST"
        
        form = AuthenticationForm(None, request.POST)
        if form.is_valid():
            print "form valid"
            login(request, form.get_user())
            
            return HttpResponseRedirect("/")
        else:
            print "form not valid"
    else:
        form = AuthenticationForm()
    
    print "first load"
    
    if request.user.is_authenticated():
        menu = MainLoggedIn
    else:
        menu = MainLoggedOut
        
    c = {'form': form,
         'menu' : menu,
         'template_menu' : 'menu.html'}
    
    c.update(csrf(request))
    
    return render_to_response("auth/login.html",  c)



def logout_view(request):
    'Logging out and redirecting to index'
    
    logout(request)
    return HttpResponseRedirect("/")
    # return HttpResponse("Logout")
